package com.jianying.day04;

import org.apache.flink.api.common.functions.FilterFunction;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

/**
 * ClassName: Flink03_Split_Stream
 * Package: com.atguigu.day04
 * Description:
 *
 * @Author: tubage
 * @Create: 2024/4/2 10:30
 * @Version: 1.0
 */
public class Flink03_Split_Stream {
    public static void main(String[] args) throws Exception {
//        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        StreamExecutionEnvironment env = StreamExecutionEnvironment.createLocalEnvironmentWithWebUI(new Configuration());
        DataStreamSource<String> socketDS = env.socketTextStream("localhost", 8888);
        env.setParallelism(1);

        SingleOutputStreamOperator<Integer> mapDS = socketDS.map(
                (MapFunction<String, Integer>) Integer::valueOf
        );
        SingleOutputStreamOperator<Integer> evenDS = mapDS.filter(
                new FilterFunction<Integer>() {
                    @Override
                    public boolean filter(Integer integer) throws Exception {
                        return integer % 2 == 0;
                    }
                }
        ).setParallelism(2);
        SingleOutputStreamOperator<Integer> oddDS = mapDS.filter(
                new FilterFunction<Integer>() {
                    @Override
                    public boolean filter(Integer integer) throws Exception {
                        return integer % 2 == 1;
                    }
                }
        ).setParallelism(3);

        evenDS.print("偶数");
        oddDS.print("奇数");

        env.execute();
    }
}
